from django.shortcuts import render
from django.views import View

from .utils import  get_categories
from .models import ContentCategory, Content
# Create your views here.

# 首页
class IndexView(View):

    def get(self,request):

        categories = get_categories()   # 方法调用
        contents = {}  # 用来装所有广告数据的字典
        contentCategory_qs = ContentCategory.objects.all()  # 获取广告所有分类
        for category in contentCategory_qs:  # 遍历获取每一条广告
            # 将获取的每一条广告添加字典中去
            contents[category.key] = category.content_set.filter(status=True).order_by('sequence')

        context = {
            "categories": categories,
            'contents': contents

        }
        return render(request, "index.html", context)